package com.zhike.b2b.user;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saas.biz.commen.MD5;
import com.zhike.b2b.model.UserCore;

@Controller
public class UserAction {
	
	@Resource
	private IUserService us;
	
	@RequestMapping("/getuserstate.do")
	public @ResponseBody String login(@RequestParam("user_name") String userName,
			@RequestParam("pwd") String passwd, HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
	    List<UserCore> luc= us.getAdminByName(userName);
		String backString="";
		if(luc!=null && luc.size()>0){
			UserCore uc = (UserCore)luc.get(0);
			String ip = request.getRemoteAddr();
			if (!("1".equals(uc.getUserType())|| ip.equals(uc.getIp())))
				backString = "4"; //登录IP地址受限
			else
			    backString = uc.getUserState();
			if("1".equals(backString))
			{
				MD5 md5 = new MD5();
				String passwdMd5 = md5.getMD5(passwd.getBytes());
				if(passwdMd5.equals(uc.getPasswd()))
				{
					backString = "1";
					us.getUserComInfo(uc);
					session.setAttribute("OLUSER", uc);
				}else 
					backString = "0";
			}
		}else{
			backString = "NONE";
		}
		return backString;
	}
	
	@RequestMapping("/user/collectprod.do")
	public @ResponseBody String collectProd(@RequestParam("prod_id") String prodId)
	{
		return "success";
	}

	@RequestMapping("/user/session.do")
	public String getSession(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		UserCore uc = (UserCore)session.getAttribute("OLUSER");
		if(uc!=null)
		{
		  request.setAttribute("SESSION_USER_TYPE",uc.getUserType());   //用户级别	
		  request.setAttribute("SESSION_CUST_TYPE",uc.getCustType());   //企业类型	
		  request.setAttribute("SESSION_CONTACT_NAME",uc.getContactName());  //联系人 	
		  request.setAttribute("SESSION_CUST_ID",uc.getCustId());   	//企业ID  
		  request.setAttribute("SESSION_USER_ID",uc.getUserId());  	//用户ID
		  request.setAttribute("SESSION_CUST_NAME",uc.getCustName());	//企业名称
		  request.setAttribute("SESSION_USER_NAME",uc.getUserName()); //用户名
		}
		return "/jsp/user_session";
	}
}
